/* Copyright 2001,2002,2003 NAH6 BV
 * All Rights Reserved
 *
 *  $Header: /var/lib/cvs/secphone/ui/controls/VuMeter2.cpp,v 1.5 2003/11/21 12:39:24 itsme Exp $
 *
 *
 * this implements a discrete level vumeter, 
 *
 * used to display the linedelay, calltype, securestatus, signallevel, batterylevel.
 * 
 * maybe I should have called this 'StateBitmap' since this module displays one of
 * a list of specified bitmaps, depending on the state of the object.
 *
 */

#include <afxwin.h>
#include "VuMeter2.h"
#include "debug.h"

BEGIN_MESSAGE_MAP(CVuMeter2, CStatic)
    //{{AFX_MSG_MAP(CVuMeter2)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()


CVuMeter2::~CVuMeter2()
{
    for (size_t i=0 ; i<m_bitmaps.size() ; i++)
        delete m_bitmaps[i];

    m_bitmaps.clear();
}


BOOL CVuMeter2::Create(const RECT& position, CWnd* parent, int min, int max, const IntVector& rsclist)
{
    if (!CStatic::Create(NULL, WS_CHILD|SS_BITMAP|WS_VISIBLE, position, parent))
        return FALSE;
    m_min= min;
    m_max= max;
    m_level= m_min;

    m_bitmaps.resize(rsclist.size());

    for (size_t i=0 ; i<rsclist.size() ; i++) {
        m_bitmaps[i]= new CBitmap();
        m_bitmaps[i]->LoadBitmap(rsclist[i]);
    }

    UpdateBitmap();

    return TRUE;
}

void CVuMeter2::SetRange(int min, int max)
{
    m_min= min;
    m_max= max;

    if (m_level<m_min)
        m_level= m_min;
    else if (m_level>m_max)
        m_level= m_max;
}

void CVuMeter2::SetLevel(int level)
{
    if (level==m_level)
        return;

    if (level<m_min)
        m_level= m_min;
    else if (level>m_max)
        m_level= m_max;
    else
        m_level= level;

    UpdateBitmap();
}
void CVuMeter2::UpdateBitmap()
{
    if (m_bitmaps.size()==0)
        return;

    int bitmapnr= ((m_level-m_min)*m_bitmaps.size())/(1+m_max-m_min);

    // when the range is empty, default to level 0
    if (m_max==m_min)
        bitmapnr= 0;

    if (bitmapnr<0 || bitmapnr>=m_bitmaps.size())
    {
        debug("ERROR: invalid bitmapnr : min=%d max=%d level=%d  nr=%d result=%d\n",
            m_min, m_max, m_level, m_bitmaps.size(), bitmapnr);
        return;
    }
    SetBitmap(*m_bitmaps[bitmapnr]);
    RedrawWindow();
}

